package org.uba.ith;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GrupoAa {

	private static String directoryName = "/Users/matias/Documents/facultad/ith/tp1/etapa3/ith-tp1-ipu";
	private static String dataFileName = "/Users/matias/Documents/facultad/ith/tp1/etapa3/datos.csv";
	
	public static double averageLengthWord(String fileName) {
		String newFileName = directoryName + "/" + fileName;
		double totalChars = 0;
		double countWords = 0;
		
		try {
			BufferedReader in = new BufferedReader(new FileReader(newFileName));
			String str;
			String[] words;
			while ((str = in.readLine()) != null) {
			    words = str.split(" ");
			    if (!isSilence(words)) {
			    	totalChars += totalChars(words);
			    	countWords += countWords(words);
			    }
			}
		    in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return totalChars/countWords;
	}
	
	private static int totalChars(String[] words) {
		int count = 0;
		for (int i = 2; i < words.length; i++) {
			count += words[i].length();
		}
		return count;
	}

	private static int countWords(String[] words) {
		return words.length -2;
	}

	private static boolean isSilence(String[] words) {
		return "#".equals(words[2]);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		File file = new File(directoryName);
		String[] files = file.list();
		List<Double> avgLengthWord = new ArrayList<Double>();
		List<Integer> ages = new ArrayList<Integer>();
		for (int i = 0; i < files.length; i++) {
			String fileName = files[i];
			if (isSpontaneous(fileName)) {
				avgLengthWord.add(GrupoAa.averageLengthWord(fileName));
				ages.add(findAge(fileName));
			}
		}
		printAges(ages);
		System.out.println("");
		printAvgLengthWord(avgLengthWord);
		
	}

	private static void printAvgLengthWord(List<Double> avgLengthWord) {
		System.out.print("avg = c(");
		boolean first = true;
		for (Double avg : avgLengthWord) {
			if (first) {
				System.out.print(avg.doubleValue());
				first = false;
			} else {
				System.out.print("," + avg.doubleValue());
			}
		}
		System.out.print(")");
	}

	private static void printAges(List<Integer> ages) {
		System.out.print("ages = c(");
		boolean first = true;
		for (Integer age : ages) {
			if (first) {
				System.out.print(age.intValue());
				first = false;
			} else {
				System.out.print("," + age.intValue());
			}
		}
		System.out.print(")");
	}

	private static int findAge(String fileName) {
		final int NAME = 0;
		final int SPEAKER = 1;
		final int SEX = 2;
		final int AGE = 3;
		final int CITY = 4;
		
		final String separator = "-";
		int separatorIndex = fileName.indexOf(separator);
		String lastName = fileName.substring(0, separatorIndex);
		String speaker = fileName.substring(separatorIndex+1,separatorIndex+2);
		String str;
		int age = 0;
		try {
			BufferedReader in = new BufferedReader(new FileReader(GrupoAa.dataFileName));
			while ((str = in.readLine()) != null) {
				String[] cols = str.split(",");
			    if (cols[NAME].equals(lastName) && cols[SPEAKER].equals(speaker)) {
			    	age = Integer.parseInt(cols[AGE]);
			    	break;
			    }
			}
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return age;
	}

	private static boolean isSpontaneous(String nameFile) {
		return nameFile.endsWith("1.ipu");
	}

}
